package com.ysh.service;

import java.util.HashMap;
import java.util.Map;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.apache.log4j.Logger;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;


public class PersistenceManager {
	 
	  public static final boolean DEBUG = true;
	  private static Logger logger = Logger.getLogger(PersistenceManager.class);
	  private static final PersistenceManager singleton = new PersistenceManager();
	  private static final String PERSISTENCE_UNIT = "default";
	  
	  private static final String PROJECT_UNIT = "project";
	  
	  protected EntityManagerFactory emf;
	  
	  protected  LoadingCache<String, EntityManagerFactory> emfs = CacheBuilder.newBuilder()
		       .maximumSize(10)
		       .expireAfterWrite(10, TimeUnit.MINUTES)		       
		       .build(
		           new CacheLoader<String, EntityManagerFactory>() {
		             public EntityManagerFactory load(String key)throws Exception{
		               return createEntityManagerFactory(key);
		             }
		           });
	  
	  public static PersistenceManager getInstance() {
	    
	    return singleton;
	  }
	  
	  private PersistenceManager() {
		  
	  }
	 
	  public EntityManagerFactory getEntityManagerFactory() {
	
	    if (emf == null)
	      createEntityManagerFactory();
	    return emf;
	  }
	  public EntityManagerFactory getEntityManagerFactory(String unit) {
		    
		  
		    try {
				return emfs.get(unit);
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		   
			 return null;
		  }
	  public void closeEntityManagerFactory() {
	    
	    if (emf != null) {
	      emf.close();
	      emf = null;
	      logger.debug("n*** Persistence finished at " + new java.util.Date());
	    }
	  }
	  
	  protected void createEntityManagerFactory() {
	    
	    this.emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT);
	    if (DEBUG)
	    	logger.debug("n*** Persistence started at " + new java.util.Date());
	  }
	  
	  protected EntityManagerFactory createEntityManagerFactory(String db){
		  
		  System.out.println("DataBase:"+db);
		
		  EntityManagerFactory em = null;
		  Map<String,String> properties = new HashMap<String,String>();
		  properties.put("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
		  properties.put("hibernate.connection.url", "jdbc:mysql://10.12.34.61:3306/"+db);
		  properties.put("hibernate.connection.username", "root");
		  properties.put("hibernate.connection.password", "root");
		  properties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");		  
		  properties.put("hibernate.show-sql", "true");
		  properties.put("hibernate.hbm2ddl.auto", "true");
		  //emf = Persistence.createEntityManagerFactory("jpablogPUnit");
		  try{
			  em=Persistence.createEntityManagerFactory(PROJECT_UNIT,properties);
		  }catch (Exception e){
			  e.printStackTrace();
		  }
		  return  em;
	  }
	}
